package com.ys.service.kefu.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.dto.AddDto;
import com.ys.dto.EditDto;
import com.ys.entity.business.TpFbaPlanOrderDetail;
import com.ys.entity.business.TpProductBasis;
import com.ys.entity.business.TsDepotInfo;
import com.ys.entity.kefu.TpGaugeMaintenance;
import com.ys.mapper.business.DiySqlMapper;
import com.ys.mapper.kefu.TpGaugeMaintenanceMapper;
import com.ys.service.kefu.TpGaugeMaintenanceService;
import com.ys.vo.R;

@Service("tpGaugeMaintenanceService")
public class TpGaugeMaintenanceImpl implements TpGaugeMaintenanceService {
	
	@Autowired
	private TpGaugeMaintenanceMapper tpGaugeMaintenanceMapper;
	@Autowired
	private DiySqlMapper diySqlMapper;
	/**
	 * 新增
	 * @operateCompanyId operateCompanyId
	 * @return
	 */
	@Override
	@Transactional
	public R add(Map<String, Object> params) {
		TpGaugeMaintenance entity = JSON.parseObject(JSON.toJSONString(params), TpGaugeMaintenance.class);
		String s = "^[0-9]*$";
		if(!String.valueOf(params.get("num")).matches(s)){
			return R.error("输入的数量有误,请重新输入");
		}
		String m = "^[0-9]+(.[0-9]{1,2})?$";
		if(!String.valueOf(params.get("weight")).matches(m)){
			return R.error("输入的重量有误,请重新输入!");
		}
		if(!String.valueOf(params.get("length")).matches(m)){
			return R.error("输入的长度有误,请重新输入!");
		}
		if(!String.valueOf(params.get("width")).matches(m)){
			return R.error("输入的宽度有误,请重新输入!");
		}
		if(!String.valueOf(params.get("height")).matches(m)){
			return R.error("输入的高度有误,请重新输入!");
		}
		TpProductBasis tpProductBasis = new TpProductBasis()
				.setCode(String.valueOf(params.get("code")))
				.queryFirst();
		params.put("name",tpProductBasis.getName());
		entity.setName(tpProductBasis.getName());
		String warehouseAddress = entity.getWarehouseAddress();
		if (warehouseAddress == null || warehouseAddress.length() == 0 ) {
			return R.error();
		}
		tpGaugeMaintenanceMapper.insert(entity);
		return R.ok(AddDto.closeAndReload());
	}

	/**
	 * 删除
	 * @operateCompanyId operateCompanyId
	 * @return
	 */
	@Override
	@Transactional
	public R deleteTpGaugeMaintenance(Map<String, Object> params){
		String[] strs = params.get("list").toString().split(",");
		for(int i=0,len=strs.length;i<len;i++){
			//查询未审核状态下头程信息
			List<TpFbaPlanOrderDetail> tpFbaPlanOrderDetail=new TpFbaPlanOrderDetail()
					.addLeftJoin("TP_FbaPlanOrder B on B.Code=A.CodePlanOrder ")
					.where("B.State = 0 and B.IsDeliver=1 and A.GaugeMaintenanceId = "+Long.valueOf(strs[i]))
					.queryList();
			if(tpFbaPlanOrderDetail.size() != 0){
				return R.error("该头程信息已被使用");
			}
			diySqlMapper.deleteTpGaugeMaintenance(Long.valueOf(strs[i]));
		}

		return R.ok(EditDto.closeAndReload());
	}
	/**
	 * 修改
	 * @operateCompanyId operateCompanyId
	 * @return
	 */
	@Override
	@Transactional
	public R edit(Map<String, Object> params) {
		String s = "^[0-9]*$";
		if(!String.valueOf(params.get("num")).matches(s)){
			return R.error("输入的数量有误,请重新输入");
		}
		String m = "^[0-9]+(.[0-9]{1,2})?$";
		if(!String.valueOf(params.get("weight")).matches(m)){
			return R.error("输入的重量有误,请重新输入!");
		}
		if(!String.valueOf(params.get("length")).matches(m)){
			return R.error("输入的长度有误,请重新输入!");
		}
		if(!String.valueOf(params.get("width")).matches(m)){
			return R.error("输入的宽度有误,请重新输入!");
		}
		if(!String.valueOf(params.get("height")).matches(m)){
			return R.error("输入的高度有误,请重新输入!");
		}
		TpGaugeMaintenance entity = JSON.parseObject(JSON.toJSONString(params), TpGaugeMaintenance.class);

		//查询未审核状态下头程信息
		List<TpFbaPlanOrderDetail> tpFbaPlanOrderDetail=new TpFbaPlanOrderDetail()
				.addLeftJoin("TP_FbaPlanOrder B on B.Code=A.CodePlanOrder ")
				.where("B.State = 0 and B.IsDeliver=1 and A.GaugeMaintenanceId = "+entity.getId())
				.queryList();
		if(tpFbaPlanOrderDetail.size() != 0){
			return R.error("该头程信息已被使用");
		}
		tpGaugeMaintenanceMapper.update(entity);
		return R.ok(EditDto.closeAndReload());
	}

	/**
	 * 批量导入箱规维护
	 */
	@Override
	public R importInfo(String rows,Long menuId){
		JSONArray JA = JSON.parseArray(rows);
		for (int i = 0; i < JA.size(); i++) {
			JSONObject JO = JA.getJSONObject(i);
			TpGaugeMaintenance entity = JSON.parseObject(JO.toJSONString(), TpGaugeMaintenance.class);
			
			// 验证商品编码
			if(new TpGaugeMaintenance().setCode(entity.getCode()).count() > 0) {
				continue;
			} else if (new TpProductBasis().setCode(entity.getCode()).count() == 0) {
				continue;
			} else if (new TsDepotInfo().setDepotName(entity.getWarehouseAddress()).count() == 0) {
				continue;
			}
			tpGaugeMaintenanceMapper.insert(entity);
		}

		return R.ok("导入成功！");
	}
}
